package com.mobile.liuyi_s.orderlunch.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.mobile.liuyi_s.orderlunch.R;
import com.mobile.liuyi_s.orderlunch.entity.Feedback;
import com.mobile.liuyi_s.orderlunch.entity.MyUser;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;

public class FeedbackActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "FeedbackActivity";
    private TextView mTextViewUserName;
    private Spinner mSpinner;
    private EditText mEditTextFeedback;
    private Button mButtonConfirm;
    private Toolbar mToolbar;

    private String mType;
    private String mFeedback;
    private String mUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        initView();

        spinnerFunction(this, mSpinner);
        MyUser user = BmobUser.getCurrentUser(MyUser.class);
        mUsername = user.getUsername();
        mTextViewUserName.setText(mUsername);
    }

    private void initView() {
        initToolbar();
        mTextViewUserName = ((TextView) findViewById(R.id.txt_default_username_feedback_activity));
        mSpinner = ((Spinner) findViewById(R.id.spinner_feedback_activity));
        mEditTextFeedback = ((EditText) findViewById(R.id.edt_feedback_activity));
        mButtonConfirm = ((Button) findViewById(R.id.btn_feedback_activity));
        mButtonConfirm.setOnClickListener(this);
    }
    private void initToolbar() {
        mToolbar = ((Toolbar) findViewById(R.id.toolbar_feedback_activity));
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_feedback_activity:
                mFeedback = mEditTextFeedback.getText().toString().trim();
                mButtonConfirm.setClickable(false);
                if (mFeedback.length() > 0) {
                    Feedback feedback = new Feedback();
                    feedback.setUsername(mUsername);
                    feedback.setType(mType);
                    feedback.setContent(mFeedback);
                    feedback.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(FeedbackActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                finish();
                            } else {
                                Toast.makeText(FeedbackActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                                mButtonConfirm.setClickable(true);
                            }
                        }
                    });

                } else {
                    Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
                break;
        }
    }

    private void spinnerFunction(final Context context, Spinner spinner) {
        String[] spinnerContent = new String[]{
                "软件使用问题",
                "饭菜质量问题",
                "饭菜口味问题",
                "其他建议"
        };
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item, spinnerContent);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Spinner resultSpinner = (Spinner) parent;
                String type = (String) resultSpinner.getItemAtPosition(position);
                mType = type;
                Log.d(TAG, "onItemSelected: " + type);
            }
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
                mType = "软件使用问题";
            }
        });
    }
}
